import msg from "../../utils/msg";
import { checkLocation } from "../../utils/util";
import { SignParamActivity, SignParamCourse, signDetail } from "./interface";

var goToSignInActivity = function (data: SignParamActivity) {
    if (!data.sr_id || data.sr_id == 0) return;//如果没有id就不调整
    wx.navigateTo({
        url: '/pages/signIn/signIn?sign_type=' + data.sign_type
            + "&sr_id=" + data.sr_id + '&activity_msg=' + JSON.stringify(data.activity_msg),
    });
}
var goToSignInCourse = function (data: SignParamCourse) {
    if (!data.sr_id || data.sr_id == 0) return;//如果没有id就不调整

    wx.navigateTo({
        url: '/pages/signIn/signIn?sign_type=' + data.sign_type + '&sr_id=' + data.sr_id + '&course_msg=' + JSON.stringify(data.course_msg),
    });
}

var _checkLocation = async function (self: any, signDetail: signDetail) {
    //检查是否在签到半径
    return new Promise(async (resolve, reject) => {
        if (self.data.gettingLocation) {
            console.log('正在获取地理位置信息，请勿重复点击');
            reject('正在获取地理位置信息，请勿重复点击');
        }
        self.setData({
            gettingLocation: true,
        });
        msg.Loading('获取地理位置中');
        let location_res = await checkLocation(signDetail.location.latitude, signDetail.location.longitude, signDetail.radius)
            .then((res: any) => {
                msg.hideLoading();
                return res;
            })
            .catch((err) => {
                msg.hideLoading();
                msg.show('获取位置信息失败', '请重试');
            });
        self.setData({
            gettingLocation: false,
        });
        if (location_res.res == false || !location_res.res) {
            reject('位置不在签到范围内:' + location_res.distance);
        }
        resolve(location_res.distance);
    })
};
var courseSignRes = function (res: any) {
    if (res.data.code == "201") {
        msg.show("已经签到过啦", ''); return;
    }
    if (res.data.code == "200") {
        let type = "签到成功";
        if (res.data.data == '迟到')
            type = '签到成功，但是迟到了';
        else if (res.data.data == '正常签到') {
            type = '签到成功';
        }
        wx.navigateTo({
            url: "/pages/signIn/signSuccess/signSuccess?tpye=" + type,
        })
    }
    if (res.data.code == '500') {
        msg.show(res.data.data[0], ''); return;
    }
}
var activitySignRes = function(res:any){
    if (res.data.code == "201") {
        msg.show("已经签到过啦", ''); return;
    }
    else if (res.data.code == "200") {
        //msg.show('签到成功', '签到状态:' + statu); return;
        wx.navigateTo({
            url: "/pages/signIn/signSuccess/signSuccess?tpye=" + res.data.data,
        });
    }
}
//'正常签到'|'迟到'|'放假'|'请假'|'待签到'
var checkCourseType=function(type:string):boolean{
    if(type!='待签到'){
        switch(type){
            case'正常签到':
            case'迟到':msg.show('已经签到过了','');break;
            case'请假':msg.show('这节课已经请假了','');break;
            case'放假':msg.show('这节课放假哦','');break;
        }
        return false;
    }
    return true;
}
export {
    goToSignInActivity,
    goToSignInCourse,
    SignParamActivity,
    SignParamCourse,
    _checkLocation,
    courseSignRes,
    activitySignRes,
    checkCourseType,
}